

/*
plot Fed balance sheet time series

input from FRED
output:
- f4a_fed_balance_*.pdf: figure of fed balance sheet over time
-- cummulative and shares

*/


* plotting options
set scheme lean_uncluttered



********************************************************************************
********************************************************************************
* QE1 and QT comparison


use ../data/fred/_fred_balance_sheet.dta, clear
drop if year(date)<2004
* balance sheet data in trillions
ds date, not
foreach var of varlist `r(varlist)' {
	replace `var' = `var'/ 1e3
}
* year-only date format for plots
format date %tdMon_YY


* combine <1Y
gen TREAS0T1 = TREAS15 + TREAS1590 + TREAS911Y

* QE1
twoway line TREAS10Y TREAS5T10 TREAS1T5 TREAS0T1 date ///
	if date>=mdy(3, 1, 2009) & date<=mdy(11, 1, 2009) , ///
	xtitle("") ytitle("Billions $") ///
	legend(order(1 ">10Y" 2 "5-10Y" 3 "1-5Y" 4 "<1Y") ///
		ring(0) position(11) rows(2) ) ///
	title("Panel A: QE1 Balance Sheet") ///
	name("fed_balance_QE1", replace)

* QE1
twoway line TREAS10Y TREAS5T10 TREAS1T5 TREAS0T1 date ///
	if date>=mdy(3, 1, 2022) & date<=mdy(11, 1, 2022) , ///
	xtitle("") ytitle("Billions $") ///
	legend(off) ///
	title("Panel B: QT Balance Sheet") ///
	name("fed_balance_QT", replace)


* combine with correct size and export
local fname f4a_fed_balance_qe1_qt
graph combine fed_balance_QE1 fed_balance_QT, ///
	rows(2) imargin(tiny) xsize(5) ysize(7) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


************************************************************
* stacked bar charts, full sample
* load data
use ../data/fred/_fred_balance_sheet.dta, clear
drop if year(date)<2004
* balance sheet data in trillions
ds date, not
foreach var of varlist `r(varlist)' {
	replace `var' = `var'/ 1e6
}
* cummulate totals/fractions for stacked line graph
* largest to smallest maturities
gen double Yg10 = TREAST
gen double Y5t10 = Yg10 - TREAS10Y
gen double Y1t5 = Y5t10 - TREAS5T10
gen double Y1 = Y1t5 - TREAS1T5

* fractions
local mat_buckets 1 1t5 5t10 g10
foreach mat of local mat_buckets {
	gen double F`mat' = Y`mat' / TREAST
}


**********************
* create plotting options for indicators for LSAP events
* QE dates
local dt_qe1T `=mdy(3,18,2009)'
local dt_qe2 `=mdy(8,10,2010)'
local dt_ot `=mdy(9,21,2011)'
local dt_qe3T `=mdy(12,12,2012)'
local dt_covid `=mdy(3,15,2020)'
* note: add final date for plots
local dts_lsap_all `dt_qe1T' `dt_qe2' `dt_ot' `dt_qe3T' `dt_covid' `=mdy(1,1,2021)'
local txt_lsap_all QE1 QE2 OT QE3 COVID
* for creating dotted lines in forefront of graphs
local plot_dts_cumm ""
local plot_dts_share ""
local plot_txts_cumm ""
local plot_txts_share ""

* year-only date format for plots
format date %tdCY

* same axis ranges for all plots
summ TREAST
local ymax = `r(max)' + 0.1
summ date
local xmin = `r(min)'
local xmax = `r(max)'
local xlabels `=mdy(1,1,2005)' `=mdy(1,1,2010)' ///
	`=mdy(1,1,2015)' `=mdy(1,1,2020)'
local ylabels 0 1 2 3 4

* "gif" version (no title)
twoway (area Yg10 date, fcolor("scheme p1") lcolor("scheme p1")) ///
	(area Y5t10 date, fcolor("scheme p2") lcolor("scheme p2")) ///
	(area Y1t5 date, fcolor("scheme p3") lcolor("scheme p3")) ///
	(area Y1 date, fcolor("scheme p4") lcolor("scheme p4")) ///
	if date<=`dt_qe1T' , ///
	xscale(r(`xmin' `xmax')) yscale(r(0 `ymax')) ///
	xtitle("") ytitle("Trillions $") xlabel(`xlabels') ylabel(`ylabels') ///
	legend(order(1 2 3 4) ///
		label(1 ">10Y") label(2 "5-10Y") ///
		label(3 "1-5Y") label(4 "<1Y") ///
		ring(0) position(11) rows(4) ) ///
	title("") ///
	name("fed_balance_cumm0", replace) nodraw

* need to create vertical line commands or the lines end up in background
forvalues i=1/5 {
	local dt_curr: word `i' of `dts_lsap_all'
	local dt_next: word `=`i'+1' of `dts_lsap_all'
	local txt: word `i' of `txt_lsap_all'
	
	* cummulative vertical lines and text
	local plot_dts_cumm `plot_dts_cumm' ///
		(scatteri  0 `dt_curr' `ymax' `dt_curr', ///
		lcolor(black) lwidth(0.15) lpattern(dash) c(l) m(i))
	local plot_txts_cumm `plot_txts_cumm' ///
		text(`=0.9*`ymax'' `=`dt_curr'-100' `"`txt'"' , ///
		size(small) orient(vertical) )
	
	* shares vertical lines and text
	local plot_dts_share `plot_dts_share' ///
		(scatteri  0 `dt_curr' 1 `dt_curr', ///
		lcolor(black) lwidth(0.15) lpattern(dash) c(l) m(i))
	local plot_txts_share `plot_txts_share' ///
		text(0.9 `=`dt_curr'-100' `"`txt'"' , ///
		size(small) orient(vertical) )
	
	* plot
	twoway (area Yg10 date, fcolor("scheme p1") lcolor("scheme p1")) ///
		(area Y5t10 date, fcolor("scheme p2") lcolor("scheme p2")) ///
		(area Y1t5 date, fcolor("scheme p3") lcolor("scheme p3")) ///
		(area Y1 date, fcolor("scheme p4") lcolor("scheme p4")) ///
		`plot_dts_cumm' if date<=`dt_next' , `plot_txts_cumm' ///
			xscale(r(`xmin' `xmax')) yscale(r(0 `ymax')) ///
			xtitle("") ytitle("Trillions $") ///
			xlabel(`xlabels') ylabel(`ylabels') ///
			legend(order(1 2 3 4) ///
				label(1 ">10Y") label(2 "5-10Y") ///
				label(3 "1-5Y") label(4 "<1Y") ///
				ring(0) position(11) rows(4) ) ///
			title("") ///
			name("fed_balance_cumm`i'", replace) nodraw
}
**********************
* create single plots with titles as well, to combine

* stacked line graph: totals (with legend)
twoway (area Yg10 date, fcolor("scheme p1") lcolor("scheme p1")) ///
	(area Y5t10 date, fcolor("scheme p2") lcolor("scheme p2")) ///
	(area Y1t5 date, fcolor("scheme p3") lcolor("scheme p3")) ///
	(area Y1 date, fcolor("scheme p4") lcolor("scheme p4")) ///
	`plot_dts_cumm' , `plot_txts_cumm' ///
		xtitle("") ytitle("Trillions $") ///
		legend(order(1 2 3 4) ///
			label(1 ">10Y") label(2 "5-10Y") ///
			label(3 "1-5Y") label(4 "<1Y") ///
			ring(0) position(11) rows(4) ) ///
	title("Panel A: Treasury Holdings Volume by Maturity") ///
	name("fed_balance_cumm", replace) nodraw

* stacked line graph: fractions (no legend)
twoway (area Fg10 date, fcolor("scheme p1") lcolor("scheme p1")) ///
	(area F5t10 date, fcolor("scheme p2") lcolor("scheme p2")) ///
	(area F1t5 date, fcolor("scheme p3") lcolor("scheme p3")) ///
	(area F1 date, fcolor("scheme p4") lcolor("scheme p4")) ///
	`plot_dts_share' , `plot_txts_share' ///
		xtitle("") ytitle("Share") legend(off) ///
	title("Panel B: Treasury Holdings Share by Maturity") ///
	name("fed_share", replace) nodraw

* combine with correct size and export
local fname f4a_fed_balance
* vertical
graph combine fed_balance_cumm fed_share, ///
	rows(2) imargin(tiny) xsize(5) ysize(7) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace

